//package com.tzy.for10evalue.common;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import io.micrometer.core.instrument.Gauge;
//import io.micrometer.core.instrument.MeterRegistry;
//import io.micrometer.core.instrument.binder.MeterBinder;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeansException;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.Map;
//import java.util.concurrent.Executors;
//import java.util.concurrent.TimeUnit;
//
///**
// * @description: DruidDataSourceMetrics  druid连接池指标接入prometheus监控
// **/
//@Configuration
//public class DruidDataSourceMetrics implements MeterBinder, ApplicationContextAware {
//    private ApplicationContext applicationContext;
//    private static final Logger logger = LoggerFactory.getLogger(DruidDataSourceMetrics.class);
//    public DruidDataSourceMetrics() {
//    }
//
//    public boolean collect(MeterRegistry meterRegistry) {
//        Map<String, DruidDataSource> datasources = applicationContext.getBeansOfType(DruidDataSource.class);
//        if (datasources.values().contains(null)) {
//            return false;
//        }
//        for (Map.Entry<String, DruidDataSource> entry : datasources.entrySet()) {
//            Gauge.builder("druid.pool.max_active" , () -> (entry.getValue().getMaxActive())).tags("datasourcename", entry.getKey()).description("druid pool max active")
//                    .register(meterRegistry);
//            Gauge.builder("druid.pool.active_count" , () -> (entry.getValue().getActiveCount())).tags("datasourcename", entry.getKey()).description("druid pool active count")
//                    .register(meterRegistry);
//            Gauge.builder("druid.pool.wait_thread_count" , () -> (entry.getValue().getWaitThreadCount())).tags("datasourcename", entry.getKey()).description("druid pool wait thread count")
//                    .register(meterRegistry);
//        }
//        return true;
//    }
//
//    @Override
//    public void bindTo(MeterRegistry meterRegistry) {
//        try {
//            //延迟30秒执行
//            Executors.newSingleThreadScheduledExecutor().schedule(() -> {
//                int i=10;
//                while (!collect(meterRegistry)) {
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    i--;
//                    if(i<0){
//                        break;
//                    }
//                }
//            }, 30, TimeUnit.SECONDS);
//        } catch (Exception e) {
//            logger.warn("druid datasource collected error",e);
//        }
//
//    }
//
//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.applicationContext = applicationContext;
//    }
//}
